<template>
  <div>
    <form action="/">
      <van-search
        @focus="isshowres=false"
        background="#3296fa"
        v-model="value"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="$router.back()"
      />
    </form>
    <!-- 搜索建议 -->
    <search-results :searchText='value' v-if="isshowres"></search-results>
     <search-history @deleteall="history=[]" @onSearch='onSearch' :history="history" v-else-if="!value"></search-history>
    <searchsuggestion @onSearch='onSearch' :search_text="value" v-else></searchsuggestion>

  </div>
</template>

<script>
import { getStorage, setStorage } from '@/utils/storage'
import SearchHistory from './components/SearchHistory.vue'
import SearchResults from './components/SearchResults.vue'
import Searchsuggestion from './components/Searchsuggestion.vue'
export default {
  components: { Searchsuggestion, SearchHistory, SearchResults },
  data () {
    return {
      value: '',
      isshowres: false,
      history: getStorage('TT-HISTORY') || []
    }
  },
  watch: {
    history: {
      deep: true,
      handler (val) {
        setStorage('TT-HISTORY', val)
      }
    }
  },

  created () {

  },

  methods: {
    onSearch (item) {
      this.value = item
      this.isshowres = true
      this.history.unshift(item)
      this.history = [...new Set(this.history)]
    },
    onCancel () {
      console.log(1)
    }
  }
}
</script>

<style scoped lang='less'>
.van-search__action{
  color: #fff;
}
</style>
